package com.thysdy.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thysdy.flowable.entity.ActIdMembership;
import com.thysdy.flowable.enums.CodeEnum;
import com.thysdy.flowable.exception.MyException;
import com.thysdy.flowable.service.IActIdMembershipService;
import com.thysdy.flowable.vo.ReturnVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ML
 * @since 2020-11-06
 */
@RestController
@RequestMapping("/act-id-membership")
public class ActIdMembershipController {
    @Resource
    IActIdMembershipService membershipService;

    @PostMapping("addMembership")
    public ReturnVo addMembership(@RequestBody List<ActIdMembership> memberships) throws MyException {
        try {
            if (null != memberships) {
                membershipService.saveBatch(memberships);
                return ReturnVo.success();
            } else {
                throw new MyException(CodeEnum.paramError);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }
    }

    @DeleteMapping("deleteMembership")
    public ReturnVo deleteMembership(@RequestBody ActIdMembership membership) throws MyException {
        try {
            if (null != membership.getUserId() && null != membership.getGroupIds()) {
                LambdaQueryWrapper<ActIdMembership> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ActIdMembership::getUserId, membership.getUserId());
                queryWrapper.in(ActIdMembership::getGroupId, membership.getGroupIds());
                membershipService.remove(queryWrapper);

                return ReturnVo.success();
            } else {
                throw new MyException(CodeEnum.paramError);
            }
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }
    }

}

